package com.dcbeida.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.DrugSaleItem;
import com.dcbeida.bean.Sales;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 销售
 */
public class SalsesAdapter extends BaseAdapter {

    private ArrayList<Sales.ListBean> mArrayList = new ArrayList<>();
    private Context mContext;

    public SalsesAdapter(Context context) {
        super();
        this.mContext = context;
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.salses_item_new, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Sales.ListBean bean = mArrayList.get(position);


//        if(position ==3){
//            bean.setHospitalstatus(2);
//        }
        holder.lin_add_drugs.removeAllViews();
        holder.tvBh.setText(mContext.getString(R.string.bh) + bean.getId());
        holder.tvTime.setText(bean.getCreatetime());
        holder.tvName.setText(bean.getTruename());
        holder.tvTel.setText(bean.getPhonenum());
        holder.tvStoreName.setText(bean.getHospitalname());
        //        status:1,	//订单1表示有效，当为退货单时，0表示未审，1表示已经审核，取消的退货单不会出现在列表中。
        if (bean.getRefundid().equals("0")) {
            //正常
            holder.tvStatus.setText("");
            holder.tvStatus.setVisibility(View.INVISIBLE);
            holder.tvMoney.setText("¥" + bean.getAmount());

            if (bean.getInfostatus() > 0 || bean.getHospitalstatus() > 0) {
                holder.ivStatus.setImageResource(R.mipmap.quehuo);
                holder.ivStatus.setVisibility(View.VISIBLE);
                holder.tvMoney.setText("¥" + bean.getAmount());
                holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.c_EA350C));
            } else if ((bean.getType() & (1 | 2)) > 0) {
                Log.d("TAG", "getView: =====赠药订单"+bean.getType());
                holder.ivStatus.setImageResource(R.drawable.icon_zengyao);
                holder.ivStatus.setVisibility(View.VISIBLE);
                holder.tvMoney.setText("¥" + bean.getAmount());
                holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.c_0C87EA));
            } else {

                holder.ivStatus.setVisibility(View.INVISIBLE);
                holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.c_0C87EA));
            }

        } else {
            //退单
            if (bean.getStatus().equals("0")) {
                //审核中
                holder.tvStatus.setText(R.string.shz);
                holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_1AAD19));
            } else {
                //退款成功
                holder.tvStatus.setText(R.string.tkcg);
                holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_999999));
            }
            holder.tvMoney.setText("-¥" + bean.getAmount());
            holder.tvMoney.setTextColor(mContext.getResources().getColor(R.color.c_EA350C));
            holder.ivStatus.setImageResource(R.mipmap.tuohuo);
            holder.tvStatus.setVisibility(View.VISIBLE);
            holder.ivStatus.setVisibility(View.VISIBLE);
//            holder.tvTui.setVisibility(View.VISIBLE);
        }
        if(bean.getDruglist() != null && bean.getDruglist().size()>0){
            for (DrugSaleItem drugSaleItem : bean.getDruglist()) {
                View inflate = LayoutInflater.from(mContext).inflate(R.layout.drug_sale_item, null);
                TextView view_tv_name = (TextView) inflate.findViewById(R.id.tv_name);
                TextView view_tv_count = (TextView) inflate.findViewById(R.id.tv_count);
                view_tv_name.setText(drugSaleItem.getDrugname());
                if (bean.getRefundid().equals("0") && (bean.getInfostatus() > 0 || bean.getHospitalstatus() > 0)) {
                    view_tv_count.setTextColor(mContext.getResources().getColor(R.color.c_EA350C));
                    view_tv_count.setText(drugSaleItem.getQuantity()+"盒");
                }else if(!bean.getRefundid().equals("0")){
                    view_tv_count.setTextColor(mContext.getResources().getColor(R.color.c_EA350C));
                    view_tv_count.setText("-"+drugSaleItem.getQuantity()+"盒");
                }else{
                    view_tv_count.setText(drugSaleItem.getQuantity()+"盒");
                }

                holder.lin_add_drugs.addView(inflate);
            }
        }

        return convertView;
    }


    public ArrayList<Sales.ListBean> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(ArrayList<Sales.ListBean> mArrayList) {
        if (mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void clearList() {
        this.mArrayList.clear();
    }


    class ViewHolder {
        @Bind(R.id.tv_bh)
        TextView tvBh;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_tel)
        TextView tvTel;
        @Bind(R.id.tv_money)
        TextView tvMoney;
        @Bind(R.id.tv_store_name)
        TextView tvStoreName;
        @Bind(R.id.lin_add_drugs)
        LinearLayout lin_add_drugs;

        //        @Bind(R.id.tv_num)
//        TextView tvNum;
//        @Bind(R.id.tv_tui)
//        TextView tvTui;
        @Bind(R.id.iv_status)
        ImageView ivStatus;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
